﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            new TaskFactory().StartNew(() =>
            {
                var p = Server.GetIns();
                Console.WriteLine(p.Name);

            });
            new TaskFactory().StartNew(() =>
            {
                Helper.ManualReset.WaitOne();
                var p = Server.GetIns();
                Console.WriteLine(p.Name);

            });
            Console.Read();
        }
    }
    public class Server
    {
        private static object Obj = new object();
        public static P pa;

        public static P GetIns()
        {
            if (pa == null)
            {
                lock (Obj)
                {
                    if (pa == null)
                    {
                        pa = new P("测试");
                    }
                }
            }

            return pa;
        }
    }

    public class Helper
    {
        public static ManualResetEvent ManualReset = new ManualResetEvent(false);
    }
    class P
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                Helper.ManualReset.Set();
                //Thread.Sleep(5000);

                _name = value;
            }
        }

        public P(string name)
        {
            Name = name;
        }
    }


}
